const LOCAL_KEY = "todomvc";


/**
 * 生成一个任务的唯一编号，时间戳+4位随机数
 */
export function generateId() {
    return Date.now() + Math.random().toString(16).slice(2, 6);
}
/**
 * 
 * 获取目前所有的任务
 */
export function fetch() {
    const result = localStorage.getItem(LOCAL_KEY);
    if(result) {
        return JSON.parse(result);
    }
    return [];
}

/**
 * 
 * @param {*} todos 任务列表 
 */
export function save(todos) {
    localStorage.setItem(LOCAL_KEY, JSON.stringify(todos));
}

export function filter(todos, visbility = "all") {
    if(visbility === "all") {
        return todos;
    } else if(visbility === "active") {
        return todos.filter((item) => !item.completed);
    } else if(visbility === "completed") {
        return todos.filter((item) => item.completed);
    }
    throw new Error("invalid visbility value");
}